<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


<div class="layui-fluid">
    <div class="layui-card">

        <div class="layui-card-header" style="color:#d02e25;font-family: '楷体'; ">
            课程管理
        </div>


        <div class="layui-card-body">
            <table id="lessonlist" lay-filter="lessonlist"></table>
        </div>
    </div>
</div>

<script>
    layui.use(['table','form'], function(){
        var table = layui.table
            ,form = layui.form;
        var laydate = layui.laydate;

        //农资列表
        table.render({
            elem: '#lessonlist'
            // ,height: 'full-200'
            ,method: 'post'
            ,url: '/ajax/party/admin/lessonlist'
            ,page: true
            ,loading: true
            ,cellMinWidth: 80
            ,limit: 15
            ,id: 'lessonlist'
            ,cols: [[
                {field: 'id', title: 'ID', width: 80, sort: true, fixed: 'left'},
                {field: 'name', title: '课程名', width: 100, align:'center'}
                ,{field: 'typeOne',templet:'#typeOne', title: '课程一类 ', align:'center'}
                ,{field: 'typeTwo',templet:'#typeTwo', title: '课程二类', align:'center'}
                ,{field: 'theSource', title: '介质来源', align:'center'}
                ,{field: 'uploadUnit', title: '上传单位 ', align:'center'}
                ,{field: 'uploadDate', title: '上传时间', align:'center'}
                ,{field: 'checkDate', title: '审核时间 ', align:'center'}
                ,{field: 'status', templet:'#status',title: '状态 ',width:80, align:'center'}
                ,{field: 'ispublic',templet:'#ispublic', title: '是否公开 ', align:'center'}
                ,{fixed: 'right', title: '操作', align: 'center', width: 120, toolbar: '#dothing' }
            ]]
        });

        //监听工具条
        table.on('tool(lessonlist)', function(obj){
            var data = obj.data;

            if(obj.event === 'detail'){

                $.post('/ajax/party/admin/getOneLessonn',{
                    id:data.id
                },function(result){
                    layer.open({
                        type: 1,
                        area: ['1000px','80%'],
                        title: "ID:" + data.id,
                        offset: '10%',
                        anim: 4,
                        shadeClose: true,
                        content:showDetails(result.data)
                    });

                    //将复选框默认赋值
                    var check1 = result.data.typeOne;
                    var check2 = result.data.typeTwo;
                    var check3 = result.data.status;
                    var check4 = result.data.ispublic;

                    if(check1==1){

                        $('#typeOne2').attr("checked",true);

                    }
                    if(check2==1){

                        $("#typeTwo2").attr("checked",true);

                    }
                    if(check3==1){

                        $("#status2").attr("checked",true);

                    }
                    if(check4==1){

                        $("#ispublic2").attr("checked",true);

                    }

                    form.render();


                    //获取党组织
                    $.post("/ajax/party/admin/branches",{
                        check:1024
                    },function (res) {
                        //得到所有级别
                        //输出
                        console.log(res);
                        var index = 0;
                        res = res.data;
                        for(i = 0; i < res.length; i++){
                            if(res[i].branchName == result.data.uploadUnit){
                                var obj=document.getElementById('uploadUnit');
                                obj.options[obj.options.length]=new Option(res[i].branchName,res[i].branchName);
                                $("#uploadUnit>option").eq(obj.options.length-1).attr("selected", true);
                                index = obj.options.length-1;
                            }else {
                                document.getElementById("uploadUnit").options.add(new Option(res[i].branchName,res[i].branchName));
                            }
                        }

                        form.render('select');
                    });

                    laydate.render({
                        elem: '#uploadDate'
                    });

                    laydate.render({
                        elem: '#checkDate'
                    });


                    form.render();

                    //提交信息
                    form.on('submit(ok)',function (data) {
                        console.log(data.field);
                        //设置id
                        data.field.id = obj.data.id;
                        //将数据转换成是否
                        if(data.field.typeOne=="on"){
                            data.field.typeOne = 1;
                        }else{
                            data.field.typeOne = 0;
                        }

                        if(data.field.typeTwo=="on"){
                            data.field.typeTwo =1;
                        }else{
                            data.field.typeTwo = 0;
                        }

                        if(data.field.ispublic=="on"){
                            data.field.ispublic = 1;
                        }else{
                            data.field.ispublic = 0;
                        }

                        if(data.field.status=="on"){
                            data.field.status = 1;
                        }else{
                            data.field.status = 0;
                        }


                        console.log(data.field);
                        $.ajax({
                            type:"post",
                            url:"/ajax/party/admin/addlesson",
                            contentType : 'application/json;charset=utf-8',
                            //设置请求头信息
                            dataType:"json",
                            data:JSON.stringify(data.field),
                            success:function (res) {
                                if(res.code==0){
                                    layer.alert("OK");
                                }else {
                                    layer.alert("发生了一些小错误，提交失败！");
                                }
                            }
                        })

                        return false;
                    })
                });
            } else if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    $.post("/ajax/party/admin/deleteLesson",{
                        id:data.id
                    },function(result){
                        console.log(result);
                        if(result.code==0){
                            layer.alert(result.msg);
                            obj.del();
                        }else {
                            layer.alert("操作失败！");
                        }
                        layer.close(index);
                    });
                });
            }
        });





    });
</script>

<script type="text/html" id="dothing">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>

    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script type="application/javascript">
    function showDetails(data) {

        var s ='<form class="layui-form" id="form1">\n' +
            '        <div class="layui-card">\n' +
            '            <div class="layui-card-body">\n' +
            '\n' +
            '\n' +
            '                <div class="layui-form-item layui-form-text">\n' +
            '                    <label class="layui-form-label">课程名称</label>\n' +
            '                    <div class="layui-input-inline">\n' +
            '                        <input type="text" name="name" value='+ data.name +' lay-verify="required"\n' +
            '                               autocomplete="off" class="layui-input">\n' +
            '                    </div>\n' +
            '\n' +
            '                    <label class="layui-form-label">来源</label>\n' +
            '                    <div class="layui-input-inline">\n' +
            '                        <input type="text" name="theSource" value='+ data.theSource +'  lay-verify="required"\n' +
            '                               autocomplete="off" class="layui-input">\n' +
            '                    </div>\n' +
            '\n' +
            '                    <label class="layui-form-label">上传单位</label>\n' +
            '                    <div class="layui-input-inline">\n' +
            '                        <div class="layui-inline" style="width: 100%;">\n' +
            '\n' +
            '                            <div class="layui-input-inline" style="width: 100%;">\n' +
            '                                <select name="uploadUnit" id="uploadUnit" lay-verify="required" lay-search="" style="width: 100%;">\n' +
            '\n' +
            '                                </select>\n' +
            '                            </div>\n' +
            '                        </div>\n' +
            '                    </div>\n' +
            '\n' +
            '                </div>\n' +
            '\n' +
            '\n' +
            '                <div class="layui-form-item">\n' +
            '                    <label class="layui-form-label">上传时间</label>\n' +
            '                    <div class="layui-input-inline">\n' +
            '                        <input type="text" name="uploadDate" value='+ data.uploadDate +' id="uploadDate" lay-verify="required"\n' +
            '                               autocomplete="off" class="layui-input">\n' +
            '                    </div>\n' +
            '\n' +
            '                    <label class="layui-form-label">审核时间</label>\n' +
            '                    <div class="layui-input-inline">\n' +
            '                        <input type="text" name="checkDate" value='+ data.checkDate +' id="checkDate" lay-verify="required"\n' +
            '                               autocomplete="off" class="layui-input">\n' +
            '                    </div>\n' +
            '                </div>\n' +
            '\n' +
            '\n' +
            '\n' +
            '\n' +
            '\n' +
            '                <div class="layui-form-item">\n' +
            '                    <label class="layui-form-label">&nbsp;</label>\n' +
            '                    <div class="layui-input-block">\n' +
            '                        <input type="checkbox" name="typeOne" id="typeOne2" title="是否属于一类">\n' +
            '                        <input type="checkbox" name="typeTwo" id="typeTwo2" title="是否属于二类">\n' +
            '                        <input type="checkbox" name="status" id="status2" title="是否开设">\n' +
            '                        <input type="checkbox" name="ispublic" id="ispublic2" title="是否公开">\n' +
            '                    </div>\n' +
            '                </div>\n' +
            '\n' +
            '\n' +
            '\n' +
            '\n' +
            '\n' +
            '                <div class="layui-form-item" style="margin-left: 440px;">\n' +
            '                    <div class="layui-input-block">\n' +
            '                        <button class="layui-btn" lay-submit lay-filter="ok">提交</button>\n' +
            '                        <button type="reset" class="layui-btn layui-btn-primary" id="reset">重置</button>\n' +
            '                    </div>\n' +
            '                </div>\n' +
            '\n' +
            '\n' +
            '            </div>\n' +
            '        </div>\n' +
            '    </form>';


        return s;
    }


</script>
<script type="text/html" id="typeOne">

    {{#  if( d.typeOne == 1){ }}
    {{ d.abdStatus='是' }}
    {{#  }  }}
    {{#  if( d.typeOne == 0){ }}
    {{ d.abdStatus='否' }}
    {{#  }  }}

</script>

<script type="text/html" id="typeTwo">

    {{#  if( d.typeTwo == 1){ }}
    {{ d.abdStatus='是' }}
    {{#  }  }}
    {{#  if( d.typeTwo == 0){ }}
    {{ d.abdStatus='否' }}
    {{#  }  }}

</script>

<script type="text/html" id="status">

    {{#  if( d.status == 1){ }}
    {{ d.status='开设中' }}
    {{#  }  }}
    {{#  if( d.status == 0){ }}
    {{ d.status='未开设' }}
    {{#  }  }}

</script>

<script type="text/html" id="ispublic">

    {{#  if( d.ispublic == 1){ }}
    {{ d.ispublic='是' }}
    {{#  }  }}
    {{#  if( d.ispublic == 0){ }}
    {{ d.ispublic='否' }}
    {{#  }  }}

</script>

</div>